#ifndef _I2C_MASTER_H_
#define _I2C_MASTER_H_

#include "stdint.h"
#include "stdbool.h"

#define MY_DELAY_US  Delay_us
#define Delay_ms  delay_ms

/* Definition of GPIOI2C constants */
#define GPIOI2C_ACK  				((uint8_t)1)
#define GPIOI2C_NACK 				((uint8_t)0)
#define I2CWRITE_MASK  			((uint8_t)0xFE)
#define I2CREAD_MASK   			((uint8_t)0x01)


/***************************** User define ***********************************/
#define GPIOI2C_SCL_GPIO_PORT          GPIOA
#define GPIOI2C_SCL_PIN                GPIO_Pin_5
#define GPIOI2C_SCL_BUSCLK             RCC_AHBENR_GPIOA
#define GPIOI2C_SCL_GPIO_CLK_ENABLE()  RCC_AHBPeriphClockCmd(GPIOI2C_SCL_BUSCLK, ENABLE);

#define GPIOI2C_SDA_GPIO_PORT          GPIOA
#define GPIOI2C_SDA_PIN                GPIO_Pin_4
#define GPIOI2C_SDA_BUSCLK             RCC_AHBENR_GPIOA
#define GPIOI2C_SDA_GPIO_CLK_ENABLE()  RCC_AHBPeriphClockCmd(GPIOI2C_SDA_BUSCLK, ENABLE);

/* Definition of Macros for bit operations */
#define i2c_set_scl()      { GPIOI2C_SCL_GPIO_PORT->BSRR = GPIOI2C_SCL_PIN; }
#define i2c_clear_scl()    { GPIOI2C_SCL_GPIO_PORT->BRR = GPIOI2C_SCL_PIN; }

#define i2c_set_sda()      { GPIOI2C_SDA_GPIO_PORT->BSRR = GPIOI2C_SDA_PIN; }
#define i2c_clear_sda()    { GPIOI2C_SDA_GPIO_PORT->BRR = GPIOI2C_SDA_PIN; }
#define i2c_get_sda()      ( GPIOI2C_SDA_GPIO_PORT->IDR & GPIOI2C_SDA_PIN )
#define sda_wr_control(b)  { if(b & mask) i2c_set_sda() else i2c_clear_sda(); }
#define sda_rd_control(b)  { if(i2c_get_sda() != 0) b |= mask; }
/*********************************************************************************************************/

/** @defgroup  
  * I2C transaction result
  */ 
typedef enum 
{
 GPIOI2C_XFER_LASTNACK     =    ((uint8_t)0x00),   	 	/* !< No error              */
 GPIOI2C_XFER_ADDRNACK   	=		((uint8_t)0x01) ,   	  /* !< No Device             */
 GPIOI2C_XFER_ABORTNACK  	=		((uint8_t)0x02) ,   	  /* !< NACK before last byte */
 GPIOI2C_XFER_LASTACK    =			((uint8_t)0x04) ,    	/* !< ACK last byte         */
 GPIOI2C_XFER_BUSERR   =  			((uint8_t)0x10) ,    	/* !< Bus error             */
}I2C_Trans_Type;
/* Exported functions --------------------------------------------------------*/
void GPIOI2C_Bus_Init(void);

void I2C_Start(void);
void I2C_reStart(void);
void I2C_Stop(void);
uint8_t I2C_master_write(uint8_t b);
uint8_t I2C_master_read(uint8_t ack);


#endif